#include "DecalsManager.h"
#include "CustomModel.h"


DecalsManager::DecalsManager(void)
{
	//prvPosA.Set(-1,-1);
	//wheels = new RearWheels();
	//wheels->left.Set(-1,-1);
	//wheels->right.Set(-1,-1);
}

DecalsManager::~DecalsManager(void)
{
}

void DecalsManager::addCar( Car *c)
{
	cars.push_back(c);
	RearWheels *w = new RearWheels();
	w->left.Set(-1,-1);
	w->right.Set(-1,-1);
	wheels.push_back(w);
}

void DecalsManager::update()
{
	/*if(!tire)
	{
		tire = create_bitmap(4, 5);
		rectfill(tire, 0, 0, 4, 5, makecol(10,10,10) );
	}*/

	std::list <Bomb *>::iterator it;

	//for(it = bombList.begin(); it != bombList.end();++it)
	it = bombList.begin(); 
	while(!bombList.empty())
	{
		if( (*it)->expl )
		{
			drawExplosion(*it);
			it = bombList.erase(it);			
		}
		if(it == bombList.end() || ++it == bombList.end())
			break;
	}

	for(unsigned int i = 0; i < cars.size(); ++i)
	{
		if(cars[i]->body->GetAngularVelocity() > 4.3 || cars[i]->body->GetAngularVelocity() < -4.3)
			drawSkidMarks(cars[i], wheels[i]);
		else
			wheels[i]->left.Set(-1, -1);
	}
}

void DecalsManager::drawExplosion(Bomb *b)
{
	set_alpha_blender();
	//set_color_conversion(COLORCONV_NONE);
	draw_trans_sprite( target->src,  (BITMAP *)find_datafile_object(CustomModel::getModel()->data, "expl_mark_30")->dat, b->x - 15, b->y-15);
}

void DecalsManager::drawSkidMarks(Car *c, RearWheels *wheels)
{

	b2Vec2 p1(-1, 1.8);
	b2Vec2 p2(1, 1.8);
	p1 = c->body->GetWorldPoint(p1);
	p1 *= Config::PHYSIC_SCALE;
	p2 = c->body->GetWorldPoint(p2);
	p2 *= Config::PHYSIC_SCALE;

	RearWheels w(*wheels);
	wheels->left = p1;
	wheels->right = p2;


	//rectfill(target->src, p1.x - 6, p1.y-3, p1.x, p1.y, makecol(0,0,0) );
		//rotate_sprite(target->src, tire, p1.x, p1.y, ftofix( radToBin( car->body->GetAngle() ) ) );
		//rotate_sprite( target->src, tire, p2.x, p2.y,  ftofix( radToBin( car->body->GetAngle() ) ) );
		//rectfill(target->src, p1.x - 2, p1.y-3, p1.x + 2, p1.y, makecol(0,0,0) );
		//prvPosA = p1;
		//prvPosB = p2;

	if(w.left.x != -1)
	{

		int points1 [8] = {p1.x, p1.y, w.left.x, w.left.y, 0, 0, 0, 0};
		int points2 [8] = {p2.x, p2.y, w.right.x, w.right.y, 0, 0, 0, 0};


		b2Vec2 u = w.left - p1;
		double t = u.x;
		u.x = -1 * u.y;
		u.y = t;
		u *= 2/u.Length();

		p1 -= u;
		w.left -= u;

		u = w.right - p2;
		u.x = -1 * u.y;
		u.y = t;
		u *= 2/u.Length();

		p2 += u;
		w.right += u;

		points1[6] = p1.x; points1[7] = p1.y; points1[4] = w.left.x; points1[5] = w.left.y;
		points2[6] = p2.x; points2[7] = p2.y; points2[4] = w.right.x; points2[5] = w.right.y;


		//line(target->src, p1.x, p1.y, w.left.x, w.left.y, makeacol(0, 0, 0, 100));
		//line(target->src, p2.x, p2.y, w.right.x, w.right.y, makeacol(0, 0, 0, 100));
		//drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);
		polygon(target->src, 4, points1, makeacol(0, 0, 0, 150));
		polygon(target->src, 4, points2, makeacol(0, 0, 0, 150));

		//w.left.Set(points1[0], points1[1]);
		//prvPosB.Set(points2[0], points2[1]);
	}

	//prvPosA = p1;
	//prvPosB = p2;
}